package opa

import (
	"github.com/open-policy-agent/opa/rego"
	"gitverse.ru/synapse/kubelatte/pkg/observability/logs"
	"go.uber.org/zap"
	"reflect"
	"testing"
)

func TestPrecompile(t *testing.T) {
	type args struct {
		module string
	}
	tests := []struct {
		name    string
		args    args
		wantNil bool
		wantErr bool
	}{
		{
			name: "valid-rule",
			args: args{
				module: "import future.keywords.contains\nimport future.keywords.if\nimport future.keywords.in\n\nviolation contains {\"msg\": msg} if {\n  params := object.get(input, \"parameters\", {})\n  name := object.get(params, \"name\", [])\n  objName := input.review.name\n  objName == name\n  msg := sprintf(\"Creating this pod is prohibited! Do not create %v\", [objName])\n}",
			},
			wantNil: false,
			wantErr: false,
		},
		{
			name: "invalid-rule",
			args: args{
				module: "go StartKubelatte()",
			},
			wantNil: true,
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			logs.Logger = zap.NewNop()
			got, err := Precompile(tt.args.module)
			if (err != nil) != tt.wantErr {
				t.Errorf("Precompile() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if reflect.DeepEqual(got, rego.PreparedEvalQuery{}) != tt.wantNil {
				t.Errorf("Precompile() got = %v, want %v", got, tt.wantNil)
			}
		})
	}
}
